#ifndef __BSP_H
#define __BSP_H

#include <Includes.h>

typedef uint32_t u32;
typedef uint16_t u16;
typedef uint8_t u8;

typedef struct
{

    uint32_t NSS;
    uint32_t CLKPolarity;
    uint32_t CLKPhase;
    uint32_t DataSize;
    uint32_t FirstBit;
} spi_cfg_t;

#if defined(MS1642_BOARD)
#define SPI_NSS_PIN GPIO_PIN_0
#define SPI_NSS_GPIO GPIOB

#define SPI_MISO_PIN GPIO_PIN_0
#define SPI_MISO_GPIO GPIOA
#define SPI_MISO_AF GPIO_AF10_SPI1
// AF
#elif defined(MS1643_BOARD)
#define SPI_NSS_PIN GPIO_PIN_4
#define SPI_NSS_GPIO GPIOA

#define SPI_MISO_PIN GPIO_PIN_7
#define SPI_MISO_GPIO GPIOA
#define SPI_MISO_AF GPIO_AF10_SPI1
#else // no default value
#error "No define board!!"
#endif

#define SPI_MOSI_PIN GPIO_PIN_3
#define SPI_MOSI_GPIO GPIOA
#define SPI_MOSI_AF GPIO_AF10_SPI1

#define SPI_CLK_PIN GPIO_PIN_5
#define SPI_CLK_GPIO GPIOA
#define SPI_CLK_AF GPIO_AF0_SPI1

#define IRQ_PIN GPIO_PIN_1
#define IRQ_GPIO GPIOB
#define IRQ_EXTNUMBER EXTI0_1_IRQn
#define IRQ_HANDLER EXTI0_1_IRQHandler

// USART1
#define USARTx USART1
#define USARTx_CLK_ENABLE() __HAL_RCC_USART1_CLK_ENABLE()
#define USARTx_RX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define USARTx_TX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()

#define USARTx_FORCE_RESET() __HAL_RCC_USART1_FORCE_RESET()
#define USARTx_RELEASE_RESET() __HAL_RCC_USART1_RELEASE_RESET()

/* Definition for USARTx Pins */
#define USARTx_TX_PIN GPIO_PIN_6
#define USARTx_TX_GPIO_PORT GPIOB
#define USARTx_TX_AF GPIO_AF0_USART1
#define USARTx_RX_PIN GPIO_PIN_7
#define USARTx_RX_GPIO_PORT GPIOB
#define USARTx_RX_AF GPIO_AF0_USART1

/* Definition for USARTx's NVIC */
#define USARTx_IRQn USART1_IRQn
#define USARTx_IRQHandler USART1_IRQHandler

#define KEY_PIN GPIO_PIN_5
#define KEY_GPIO GPIOB
#define KEY_EXTNUMBER EXTI4_15_IRQn
#define KEY_HANDLER EXTI4_15_IRQHandler

#define LED_PIN GPIO_PIN_12
#define LED_GPIO GPIOA

#define dfRunningMode_Normal 0x00  // 运行状态，全速
#define dfRunningMode_Sleep 0x01   // 运行状态，蓝牙工作，MCU间断性唤醒
#define dfRunningMode_Standby 0x02 // 运行状态，全进入低功耗
extern u8 u8RunningModeSet;        // 运行状态设置
extern u8 u8RunningModeStat;       // 当前运行状态

extern SPI_HandleTypeDef Spi1Handle;

void BSP_Init(void);
void LED_Toggle(void);
u8 KEY_Status(void);
void SPI_init(void);
void Configure_EXTI(void);
void IrqMcuGotoSleepAndWakeup(void);
void LPTIM_Init(void);
void Error_Handler(void);
void LPTIM_TimeOut_Reset(void);

void SetRunningMode(u8 mode);
/* extern --------------------------------------------------------------------*/
extern void mg_activate(unsigned char value);
extern u8 mg_writeBuf(u8 reg, u8 const *pBuf, u8 len);
#endif
